<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>初始数据配置-pcfcms系统安装</title>
<link rel="stylesheet" href="style/layui/css/layui.css">
<link rel="stylesheet" href="style/css/install.css">
<style>
    .start-install{padding:20px;text-align:center;}
    #installDialogContent{padding:20px;}
</style>  
</head>
<body>
<div class="layui-main">
    <h1 class="site-h1">初始数据配置</h1>
    <form class="layui-form" action="index.php?type=step5-1" method="post">
        <div class="layui-form-item" style="width:660px;margin:0 auto;">
            <label class="layui-form-label">管理员账号</label>
            <div class="layui-input-inline">
                <input type="text" name="admin_account" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" value="<?php echo $config['account']; ?>" />
            </div>
            <label class="layui-form-label">管理员密码</label>
            <div class="layui-input-inline">
                <input type="text" name="admin_password" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input" value="<?php echo $config['password']; ?>" />
            </div>
        </div>
        <div class="layui-form-item">
            <div class="btn-box">
                <a class="layui-btn layui-btn-small layui-btn-normal" href="./index.php?type=<?php echo $config['createPage']?>">上一步</a>
                <button lay-submit="" class="layui-btn layui-btn-small" lay-filter="install">开始安装</button>
            </div>
        </div>
    </form>
</div>
<div id="installDialog" style="display:none;">
    <div id="installDialogContent">
        <div class="start-install" id="install-desc">安装过程请勿关闭窗口</div>
        <div>
        <div class="layui-progress layui-progress-big" lay-filter="syncInstall" lay-showPercent="yes">
        <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>
        </div>
        <div style="text-align:center;"><p><img src="style/loading.gif" width="180px"></p></div>
    </div>
</div>
<script type="text/javascript" src="style/layui/layui.js"></script>
<script>
    layui.use(['form','layer','jquery','element'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        var element = layui.element;
        form.render();
        element.render();

        //监听提交
        form.on('submit(install)', function () 
        {
            var data = $('.layui-form').serialize();
            var url = getUrlPath();
            window_box = layer.open({
                type: 1,
                title: '开始安装',
                area: ['450px', '305px'],
                content: $("#installDialog").html()
            });
            $.ajax({
                type: 'POST',
                url: url + '?type=step5-1',
                data: data + '&type=1',
                success: function (res) {
                    if (!res.status) {
                        layer.msg(res.msg);
                        return false;
                    } else {
                        installBase(res.data.page, res.data.totalPage, data)
                    }
                },
                dataType: 'json'
            });
            return false;
        });

        //安装基础数据
        function installBase(page, total, data) 
        {
            var url = getUrlPath();
            if (page <= total) {
                $.ajax({
                    type: 'POST',
                    url: url + '?type=step5-1',
                    data: {page: page, type: 2},
                    success: function (res) {
                        if (!res.status) {
                            layer.msg(res.msg);
                            return false;
                        } else {
                            element.progress('syncInstall',res.data.percent+'%');
                            installBase(res.data.page,res.data.totalPage,data);
                        }
                    },
                    dataType: 'json'
                });
            } else {
                layer.msg("基础数据安装成功");
                installManage(data);
            }
        }

        //添加管理员账号
        function installManage(data)
        {
            var url = getUrlPath();
            element.progress('syncInstall','0%');
            $("#install-desc").html('开始添加管理员账号');
            $.ajax({
                type: 'POST',
                url: url + '?type=step5-1',
                data: data + '&type=3',
                success: function (res) {
                    if (!res.status) {
                        layer.msg(res.msg);
                        return false;
                    } else {
                        element.progress('syncInstall','100%');
                        layer.msg("安装成功", {time: 1000}, function () {
                            window.location.href=url+'?type=step5-1';
                        })
                    }
                },
                dataType: 'json'
            });
        }

        function getUrlPath()
        {
            var domain = window.location.host;
            var url = document.location.toString();
            var arrUrl = url.split("//");
            var start = arrUrl[1].indexOf("/");
            var relUrl = arrUrl[1].substring(start);
            if(relUrl.indexOf("?") != -1){
                relUrl = relUrl.split("?")[0];
            }
            return arrUrl[0]+'//'+domain+relUrl;
        }

    });
</script>
</body>
</html>